package hezi.core;



import hezi.helper.ForumGroupDBHelper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

public class ForumGroup {
	public static final String ALLFORUM = "allforum";
	
	static Map<String,ForumGroup> groupsmap = new HashMap<String,ForumGroup>();
	static boolean needUpdateCount = true;
	public List<Forum> forumslist = new ArrayList<Forum>();
	
	public static boolean isContains(String groupid){
		return groupsmap.containsKey(groupid);
	}

	int newimagecount;
	int newpostcount;
	int totalimagecount;
	int totalpostcount;
	
	public String groupname;
	public String groupid;
	public int priority;
	
	public ForumGroup(String groupid) {
		this.groupid = groupid;
		this.groupname = groupid;
		this. priority = 0;
	}

	
	public ForumGroup() {
		
	}
	
	public static Collection<ForumGroup> getAllForumGroups() {
		ForumGroup.clear();
		ForumGroup allforum = ForumGroup.createIfNotExist("allforum");
		
		for(Forum forum : Forum.forumMap.values()){
			String groups = forum.getGroups();
			if(groups.length() > 1){
				if(!groups.contains(" ")){
					String groupid = groups.trim();
					ForumGroup forumgroup = ForumGroup.createIfNotExist(groupid);
					forumgroup.forumslist.add(forum);
				}
			}
			allforum.forumslist.add(forum);
		}
		
		return groupsmap.values();
	}
	
	
	public static Collection<ForumGroup> getAllGroupsNoAll() {
		getAllForumGroups();
		
		Collection<ForumGroup> grouplist2 = new ArrayList<ForumGroup>();
		ForumGroupDBHelper.select2(grouplist2);
		return grouplist2;
	}
	
	
	
	
	private static void clear() {
		groupsmap.clear();
	}
	public Collection<Forum> getForumsInThisGroup() {
		return forumslist;
	}

	

	
	
	


	public String generateInClause(){
		String incaluse = "AND forumid IN (";
		for(Forum f : this.getForumsInThisGroup()){
			incaluse += "'" + f.getId() + "',";
		}
		incaluse = incaluse.substring(0,incaluse.length()-1) + ") ";
		return incaluse;
	}
	
	
	
	public static void setNeedUpdateCount(boolean needUpdateCount) {
		ForumGroup.needUpdateCount = needUpdateCount;
	}
	public String getId() {
		return groupid;
	}
	public String getName() {
		return groupname;
	}
	public int getSeq() {
		return priority;
	}
//	public int getNewimagecount() {
//		return newimagecount;
//	}
	public void setNewimagecount(int newimagecount) {
		this.newimagecount = newimagecount;
	}
	public int getNewpostcount() {
		return newpostcount;
	}
	public void setNewpostcount(int newpostcount) {
		this.newpostcount = newpostcount;
	}
	public int getTotalimagecount() {
		return totalimagecount;
	}
	public void setTotalimagecount(int totalimagecount) {
		this.totalimagecount = totalimagecount;
	}
	public int getTotalpostcount() {
		return totalpostcount;
	}
	public void setTotalpostcount(int totalpostcount) {
		this.totalpostcount = totalpostcount;
	}
	public static Logger logger = Logger.getLogger("");

	public static ForumGroup createIfNotExist(String groupid) {
		ForumGroup forumgroup = groupsmap.get(groupid);
		if(forumgroup == null){
			
			forumgroup =  ForumGroupDBHelper.select(groupid);
			if(forumgroup == null){
				forumgroup = new ForumGroup(groupid);
				ForumGroupDBHelper.insert(forumgroup);
			}
			else{
			}
			
//			logger.info("groupsmap.put " + groupid + " " + forumgroup.getName());
			groupsmap.put(groupid, forumgroup);
			
		}
		return forumgroup;
	}

	public static ForumGroup getGroup(String groupid) {
		return groupsmap.get(groupid);
	}
	public static Collection<ForumGroup> getAllGroups() {
		return groupsmap.values();
	}

}
